package test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import Beans.Artigiano;
import businessLogic.GestioneArtigianiMySQL;

import connessioni.MyConnection;
import db_operations.GestioneArtigiani;

public class EliminaArtigiano_Test {
	
	
	public static void main(String args[]) throws Exception {
		
		try {
			Connection con = MyConnection.getConnection();
			
			Statement st = con.createStatement();
			
			visualizza(st);
			
			ResultSet n = st.executeQuery("SELECT MAX(ID) FROM artigiani");
			if(!n.next())
				throw new Exception("Nessun artigiano nel DB");
			
			int id = n.getInt(1);
			
			GestioneArtigiani ga = new GestioneArtigianiMySQL();
			Artigiano a = new Artigiano();
			a.setId(id);
			ga.eliminaArtigiano(a);
			
			visualizza(st);
			
		}
		catch(SQLException e) {
			e.printStackTrace();
		}	
		
		catch(ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		
	}
	
	
	private static void visualizza(Statement st) throws SQLException {
		ResultSet rs = st.executeQuery("SELECT * FROM artigiani");
		
		while(rs.next()) {
			System.out.println("ID: " + rs.getInt("ID"));
			System.out.println("Nome: " + rs.getString("Nome"));
			System.out.println("Cognome: " + rs.getString("Cognome"));
			System.out.println("PIva: " + rs.getString("PIva"));
			System.out.println("Email: " + rs.getString("Email"));
			System.out.println("IDPartner: " + rs.getInt("TipoPartner"));
			System.out.println("TipoArtigiano: " + rs.getInt("TipoArtigiano"));
			System.out.println("Nazione: " + rs.getString("Nazione"));
			System.out.println("Regione: " + rs.getString("Regione"));
			System.out.println("Città: " + rs.getString("Citta"));
			System.out.println("Provincia: " + rs.getString("Provincia"));
			System.out.println("CAP: " + rs.getString("CAP"));
			System.out.println("Via: " + rs.getString("Via"));
			System.out.println("NCivico: " + rs.getString("NCivico"));
			System.out.println("PathImg: " + rs.getString("PathImg"));
			System.out.println("Note: " + rs.getString("Note"));
			
			ResultSet tel = st.executeQuery("SELECT * FROM telefono_artigiani WHERE IDArtigiano = " + rs.getInt("ID"));
			while(tel.next()) {
				System.out.println("Tel " + rs.getInt("ID") + ": " + tel.getString("Tipo") + " - " + tel.getShort("Numero"));
			}
			tel.close();
			
			System.out.println("\n\n-----------------------------------");
		}
		
		rs.close();
		
	}

}
